package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionBehaviorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionBehaviorService;

import com.heima.feigns.behavior.CollectionFeign;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionBehaviorServiceImpl extends ServiceImpl<ApCollectionBehaviorMapper, ApCollection> implements ApCollectionBehaviorService {


//    @Autowired
//    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    CollectionFeign collectionFeign;
    @Autowired
    ApArticleService apArticleService;
    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //校验参数
        if(dto==null||dto.getEntryId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //校验登录,可校验可不校验//
//        //查询文章列表
//        Long articleId = dto.getEntryId();
//        ApArticle apArticle = apArticleService.getById(articleId);


        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询行为实体
        ApBehaviorEntry apBehaviorEntry = collectionFeign.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询收藏行为
        ApCollection apCollection = getOne(Wrappers.<ApCollection>lambdaQuery()
                .eq(ApCollection::getArticleId, dto.getEntryId())
                .eq(ApCollection::getEntryId, apBehaviorEntry.getId())
        );

        if(dto.getOperation()>1||dto.getOperation()<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if(apCollection==null&&dto.getOperation().intValue()==1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"请先收藏");
        }
        if(apCollection!=null&&dto.getOperation().intValue()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"请勿重复收藏");
        }
        if (apCollection==null&&dto.getOperation().intValue()==0){
             apCollection = new ApCollection();
             apCollection.setArticleId(dto.getEntryId());
             apCollection.setCollectionTime(new Date());
             apCollection.setEntryId(apBehaviorEntry.getId());
             save(apCollection);
             return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            removeById(apCollection);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }



    }
}
